// SalesService.java
package com.ecommerce.service;

import com.ecommerce.dao.SalesDAO;
import com.ecommerce.model.SalesReport;
import com.ecommerce.util.InputUtil;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SalesService {
    private SalesDAO salesDAO;

    public SalesService() {
        this.salesDAO = new SalesDAO();
    }

    public void displaySalesDashboard() {
        double dailyRevenue = salesDAO.getDailyRevenue();
        int dailyOrders = salesDAO.getDailyOrders();

        System.out.println("\n=== 销售仪表盘 ===");
        System.out.printf("今日营收: ¥%.2f\n", dailyRevenue);
        System.out.printf("今日订单: %d 笔\n", dailyOrders);
        System.out.println("=================");
    }

    public void generateSalesReport() {
        System.out.println("\n=== 生成销售报告 ===");

        try {
            String startInput = InputUtil.getStringInput("请输入开始日期 (yyyy-MM-dd): ");
            String endInput = InputUtil.getStringInput("请输入结束日期 (yyyy-MM-dd): ");

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date startDate = sdf.parse(startInput);
            Date endDate = sdf.parse(endInput);

            // 修复：直接传递java.util.Date，在DAO层处理转换
            SalesReport report = salesDAO.generateSalesReport(startDate, endDate);

            displaySalesReport(report);

        } catch (Exception e) {
            System.out.println("日期格式错误！请使用 yyyy-MM-dd 格式");
        }
    }

    private void displaySalesReport(SalesReport report) {
        System.out.println("\n=== 销售统计报告 ===");
        System.out.println(report);

        System.out.println("\n--- 商品销售排行 ---");
        if (report.getProductSales().isEmpty()) {
            System.out.println("暂无销售数据");
        } else {
            int rank = 1;
            // 修复：使用显式类型声明替代var
            for (com.ecommerce.model.ProductSales product : report.getProductSales()) {
                System.out.println(rank + ". " + product);
                rank++;
            }
        }

        System.out.println("\n--- 分类销售统计 ---");
        if (report.getCategorySales().isEmpty()) {
            System.out.println("暂无分类销售数据");
        } else {
            // 修复：使用显式类型声明替代var
            for (com.ecommerce.model.CategorySales category : report.getCategorySales()) {
                System.out.println(category);
            }
        }
        System.out.println("===================");
    }

    public void manageSales() {
        while (true) {
            System.out.println("\n=== 销售统计 ===");
            System.out.println("1. 查看销售仪表盘");
            System.out.println("2. 生成销售报告");
            System.out.println("3. 返回上级菜单");

            int choice = InputUtil.getIntInput("请选择操作: ");

            switch (choice) {
                case 1:
                    displaySalesDashboard();
                    break;
                case 2:
                    generateSalesReport();
                    break;
                case 3:
                    return;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }
}